<template>
  <div>
    <el-card>
      <!-- 栏目列表 -->
      <el-button type="success" @click="adddialogVisible = true"
        >添加栏目</el-button
      >
      <!-- table表格 -->
      <el-table :data="cateList" stripe border>
        <el-table-column type="index" label="编号" width="100px">
        </el-table-column>
        <el-table-column prop="name" label="栏目名称" width="100px">
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 对话弹框 -->
    <el-dialog
      title="添加栏目"
      :visible.sync="adddialogVisible"
      width="50%"
      @close="addclosefn"
    >
      <el-form :model="formInfo" ref="addFormRef" label-width="100px">
        <el-form-item label="栏目名称" prop="name">
          <el-input v-model="formInfo.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addVisible">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCategoryAPI, addCategoryAPI } from "@/apis/category";
import { setCate } from "@/utils/localDate";

export default {
  data() {
    return {
      // 控制对话弹框
      adddialogVisible: false,
      cateList: [],
      formInfo: {
        name: "",
      },
    };
  },
  created() {
    this.getcatelist();
  },
  methods: {
    //   栏目列表
    async getcatelist() {
      const { data: res } = await getCategoryAPI();
      console.log(res);
      setCate(res.data);
      this.cateList = res.data;
    },
    //   对话宽关闭
    addclosefn() {
      console.log(1);

      this.$refs.addFormRef.resetFields();
    },
    // 点击对话框
    async addVisible() {
      const { data: res } = await addCategoryAPI(this.formInfo);
      //   console.log(res);
      this.getcatelist();
      this.adddialogVisible = false;
    },
  },
};
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px;
}
</style>
